<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/9/5
 * Time: 10:20
 */


//评论父类
abstract class module_public_comment
{
    protected $cid;
    protected $ncid;

    function __construct()
    {
        global $act;
        global $tp;

        $this->cid = $this->get_cid();
        $this->ncid = $this->get_ncid();
        $this->$act();
        $tp->assign('nav', '留言管理');
    }

    //列表
    protected function index()
    {
        global $tp;
        $size = 15;
        $page = intval($_REQUEST['page']);
        $page = $page < 1 ? 1 : $page;
        $search = Common::sfilter($_REQUEST['search']);
        $list = $this->getCommentList($this->ncid, $this->cid, $search, $page, $size);
        $tp->assign('list', $list);
        $pages = Common::set_page($size, $page, $list['count']);
        foreach ($pages as $key => $value) {
            $pages[$key]['url'] = "/" . COMPANY_PATH . "/?m=comment&page=" . $value['num'] . $list['spm'];
        }
        $tp->assign("page", $page);
        $tp->assign("size", $size);
        $tp->assign("pages", $pages);
    }

    //详情
    protected function detail()
    {
        global $Db;
        global $Base;
        global $Json;
        global $tp;
        $nid = intval($_REQUEST['nid']);
        //验证$nid是否合法
        $sql = "SELECT * FROM " . $Base->table('company_new_comment') . " WHERE id = $nid";
        $comment_list = $Db->query($sql, "Row");
        if (empty($comment_list)) {
            $nid = 0;
        }
        //前台显示
        if (empty($_POST)) {
            //展示所有分类
            $tp->assign('comment', $comment_list);
            $tp->assign('nid', $nid);
        } //后台数据处理
        else {
            $res['status'] = 0;
            $data = array(
                'chuli_yijian' => Common::sfilter($_POST['chuli_yijian']),
            );
            if (empty($data["chuli_yijian"])) {
                $res['msg'] = '处理意见不能为空';
            } else {
                $where = array(
                    'id' => $nid
                );
                $Db->update($Base->table('company_new_comment'), $data, $where);
                $res = array('status' => 1, 'msg' => '提交成功', 'href' => '/' . COMPANY_PATH . '/?m=comment');
            }
            echo $Json->encode($res);
            exit;
        }
    }

    //删除
    protected function delete()
    {
        global $Db;
        global $Base;
        global $Json;
        $id = intval($_POST['id']);
        if ($id <= 0) {
            $id = -1;
        }
        // var_dump($id);
        $res['status'] = 0;
        $Db->beginTransaction();
        try {
            $sql = "delete from " . $Base->table('company_new_comment') . " where id=$id ";
            if ($this->cid > 0) {
                $sql .= "and c_id=" . $this->cid;
            }
            $Db->execSql($sql);
            //删除他的子类
            $sql = "delete from " . $Base->table('company_new_comment') . " where pid=$id ";
            if ($this->cid > 0) {
                $sql .= "and c_id=" . $this->cid;
            }
            $Db->execSql($sql);
            $res['status'] = 1;
            $Db->commit();
        } catch (Exception $e) {
            $Db->rollback();
        }
        echo $Json->encode($res);
        exit;
    }


    //回复
    protected function reply()
    {
        global $Db;
        global $Base;
        global $Json;
        global $tp;
        $pid = intval($_REQUEST['pid']);
        //检测pid是否存在
        $sql = "select * from " . $Base->table('company_new_comment') . ' where id=' . $pid . " ";
        if ($this->cid > 0) {
            $sql .= "and c_id=" . $this->cid;
        }
        $resu = $Db->query($sql, "Row");
        if ($resu) {
            if (empty($_POST)) {
                //显示界面
                $tp->assign('comment', $resu);
                $tp->assign('act', 'reply');
                $tp->assign('pid', $pid);
                //读取以往的回复记录
                $sql = "select * from " . $Base->table('company_new_comment') . ' where pid=' . $pid;
                $list = $Db->query($sql);
                $tp->assign("list", $list);
                $tp->assign('c_id',$resu["c_id"]);
            } else {
                $res['status'] = 0;
                //提交回复
                $data = array(
                    'pid' => $pid,
                    'phone' => '',
                    'comment' => Common::sfilter($_POST['comment'], 'html'),
                    'createtime' => date("Y-m-d H:i:s", time()),
                    'name' => Common::sfilter($_POST['name']),
                    'status' => 0,
                    'chuli_yijian' => '',
                    'new_id' => $resu["new_id"],
                    'c_id'=>intval($_POST['c_id'])
                );
                if ($data["pid"] < 0) {
                    $res['msg'] = '回复id不能小于0';
                } elseif (empty($data['comment'])) {
                    $res['msg'] = '回复消息不能为空';
                } elseif (empty($data['name']) || mb_strlen($data['name']) < 0 || mb_strlen($data['name']) > 10) {
                    $res['msg'] = '回复名称长度不符合限制';
                } else {
                    $insert_id = $Db->insert($Base->table('company_new_comment'), $data);
                    $res = array('status' => 1, 'msg' => '提交成功', 'href' => "/company/admin/?m=comment&act=reply&pid=" . $pid);
                }
                echo $Json->encode($res);
                die();
            }
        } else {
            //回到列表
            Common::base_header("Location:/company/admin/?m=comment");
        }
    }

    private function getCommentList($ncid, $cid, $search, $page, $size)
    {

        $spm = '';
        $sql = "select com.*,company.name c_name,new.c_id c_n_id,new.title " .
            "from " . $GLOBALS['Base']->table("company_new_comment") . " as com " .
            'left join ' . $GLOBALS['Base']->table('company_news') . ' as new on new.id=com.new_id ' .
            'left join ' . $GLOBALS['Base']->table('company') . ' as company on company.id=com.c_id ' .
            'where 1=1 ';
//        if ($cid > 0) {
//            //新闻分类中的商家留言
//            $sql .= 'and new.c_id=' . $cid." ";
//            $spm .= "&c_id=$cid";
//        }
        if ($ncid > 0) {
            //商家自己的留言
            $sql .= 'and com.c_id=' . $ncid." ";
            $spm .= "&ncid=$ncid";
        }
        if (!empty($search)) {
            $sql .= "AND com.name LIKE '%$search%' or com.phone like '%$search%' ";
            $spm = "&search=$search";
            $GLOBALS['tp']->assign('search', $search);
        }
        $sql .= ' and com.pid=0';
        $rescount = count($GLOBALS['Db']->query($sql));
        $sql .= " ORDER BY com.id desc LIMIT " . ($page - 1) * $size . ", " . $size . "";
        $list = $GLOBALS['Db']->query($sql);
        //读取商户新闻的名称
        foreach ($list as $k => $value) {
            if (intval($value["new_id"]) == 0) {
                $sql = "select company.name from " . $GLOBALS['Base']->table('company_new_comment') . ' as com ' .
                    'left join ' . $GLOBALS['Base']->table('company') . ' as company on company.id=com.c_id ';
                $resu = $GLOBALS['Db']->query($sql, "Row");
                $list[$k]['fabu_name'] = $resu['name'];
            }
        }
        return array('list' => $list, 'count' => $rescount, 'spm' => $spm);
    }

    //重写商家id(新闻分类的商家id)
    abstract protected function get_cid();

    //重写商家新闻id(为商家自己的评论id)
    abstract protected function get_ncid();
}

?>